import { ProColumns, ProTable } from '@ant-design/pro-components';
import { message, Modal } from 'antd';
import React, { PropsWithChildren } from 'react';
import {addLog} from "@/services/logService";

interface CreateModalProps {
  modalVisible: boolean;
  columns: ProColumns<LogType.Log>[];
  onSubmit: () => void;
  onCancel: () => void;
}



/**
 * 创建数据模态框
 * @param props
 * @constructor
 */
const CreateModal: React.FC<PropsWithChildren<CreateModalProps>> = (props) => {
  const { modalVisible, columns, onSubmit, onCancel } = props;
  /**
   * 添加节点
   * @param fields
   */
  const handleAdd = async (fields: LogType.Log) => {
    const hide = message.loading('正在添加');
    await addLog({ ...fields } as LogType.LogAddRequest).then(res=>{
      message.success("添加成功")
      hide()
      onCancel();
      return true;
    }).catch(e=>{
      console.log(e.message)
      message.error("添加失败");
      hide()
      return false;
    })
    return false;
  };

  return (
    <Modal
      destroyOnClose
      title="新建"
      visible={modalVisible}
      onCancel={() => onCancel()}
      footer={null}
    >
      <ProTable<LogType.Log, LogType.Log>
        onSubmit={async (value) => {
          const success = await handleAdd(value);
          if (success) {

          }
        }}
        rowKey="id"
        type="form"
        columns={columns}
      />
    </Modal>
  );
};

export default CreateModal;
